package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.runtime.Name;

import DTO.Order;
import pageUtil.PageB;
import service.PageService;
import serviceImpl.OrderDAO;

@WebServlet(name = "order", urlPatterns = "/order")
public class OrderServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public OrderServlet() {
	}

	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String flag = null;
		flag = request.getParameter("flag");
		switch (flag) {
		case "findall":
			try {
				find_all(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case "ddxq":
			try {
				ddxq(request, response);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}
		
		protected void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			doPost(request, response);

		}

		protected void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			service(request, response);
		}
		
		@SuppressWarnings("unchecked")
		protected static void ddxq(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String ddsj = request.getParameter("ddsj");
			@SuppressWarnings("rawtypes")
			List list = new ArrayList<>();
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			
			ddsj = ddsj.substring(0, ddsj.length()-1);
			String a[] = ddsj.split("[=]");
			for(int i=0;i<a.length;i++) {
				list.add(a[i]);
			}
			for(int i=0;i<list.size();i++) {
				Map<String, Object> map = new HashMap<String,Object>();
				String aString[] = list.get(i).toString().split("\\*");
				System.out.println(aString.length);
				for (int j=0;j<aString.length;j++) {
					System.out.println(aString[j]);
				}
				map.put("name", aString[0]);
				map.put("money", aString[2]);
				map.put("number", aString[4]);
				map.put("jz", aString[6]);
				data.add(map);
			}
			for(int i = 0;i<data.size();i++) {
				System.out.println(data.get(i));
			}
			request.setAttribute("msg", "修改成功");
			request.setAttribute("data", data);
			request.getRequestDispatcher("ddsj.jsp").forward(request, response);
		}
		
		
		protected static void find_all(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String sql = "select a.id,a.ddsj,a.cjsj,a.zj,b.`name` from `order` a,employee b where a.ddcjrid=b.id limit ?,?";
			String sql2 = "select count(id) from `order`";
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			String currentPage = request.getParameter("cp");
			currentPage = currentPage == null ? "1" : currentPage;
			PageB<Order> pageBean = new PageB<Order>();
			pageBean.setCurrentPage(Integer.parseInt(currentPage));
			pageBean.setPageSize(5);
			PageService<Order> pageservice = new PageService<Order>(new OrderDAO());
			PageB<Order> bean = pageservice.paging(pageBean, sql, sql2);
			String page = JSON.toJSONString(bean);
			PrintWriter out = response.getWriter();
			out.print(page);
			out.flush();
		}
}
